package com.atguigu.webadmin.schedule;

import com.atguigu.model.enums.LeaseStatus;
import com.atguigu.model.pojo.LeaseAgreement;
import com.atguigu.webadmin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName: ScheduledTasks
 * Package: com.atguigu.webadmin.schedule
 * Description: 定义定时逻辑类
 *
 * @Author 梅超凡
 * @Create 2024/11/15 20:31
 * @Version 1.0
 */
@Component
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;


    /**
     * @Author 梅超凡
     * @Date 2024/11/15~20:34
     * @Description 该方法 checkLeaseStatus 将在每天的午夜（00:00:00）执行一次
    */
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus(){
        //创建一个Lambda更新包装类对象
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        //设置查询条件
        Date now = new Date();
        //le:表示小于等于
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
        //in:表示在指定的范围内
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        //设置要修改的字段
        updateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        //执行修改
        leaseAgreementService.update(updateWrapper);
    }
}
